package com.voltbarn.vbsensors;

import java.util.Random;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class VbDBAdapter {

	static Random random = new Random();
    public static final String KEY_SENSORS = "sensors";
    public static final String KEY_STYPE = "stype";
    public static final String KEY_ROWID = "_id";

    private static final String TAG = "SensorsDbAdapter";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;

    /**
     * Database creation sql statement
     */
    private static final String DATABASE_CREATE =
            "create table tblSensorData (_id integer primary key autoincrement, "
                    + "sensors text not null, stype text not null);";

    private static final String DATABASE_NAME = "VBSensors";
    private static final String DATABASE_TABLE = "tblSensorData";
    private static final int DATABASE_VERSION = 3;

    private final Context mCtx;

    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS tblSensorData");
            onCreate(db);
        }
    }

    /**
     * Constructor - takes the context to allow the database to be
     * opened/created
     *
     * @param ctx the Context within which to work
     */
    public VbDBAdapter(Context ctx) {
        this.mCtx = ctx;
    }

    public VbDBAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }
    
    public VbDBAdapter openRO() throws SQLException {
    	mDbHelper = new DatabaseHelper(mCtx);
    	mDb = mDbHelper.getReadableDatabase();
    	return this;
    }

    public void close() {
        mDbHelper.close();
    }
    public long createSensor(String sensors, String stype) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_SENSORS, sensors);
        initialValues.put(KEY_STYPE, stype);

        return mDb.insert(DATABASE_TABLE, null, initialValues);
    }
    public boolean deleteSensor(long rowId) {

        return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }
    public Cursor fetchAllSensors() {

        return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_SENSORS, KEY_STYPE}, null, null, null, null, null);
    }

    public Cursor fetchSensor(long rowId) throws SQLException {

        Cursor mCursor =

                mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
                        KEY_SENSORS, KEY_STYPE}, KEY_ROWID + "=" + rowId, null,
                        null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;

    }

    public boolean updateSensor(long rowId, String title, String sensor) {
    	ContentValues args = new ContentValues();
    	args.put(KEY_SENSORS, title);
    	args.put(KEY_STYPE, sensor);

    	return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
    }
    

    public int getNumEntries()
    {
        Cursor cursor = mDb.rawQuery(
                    "SELECT COUNT(sensors) FROM tblSensorData", null);
        int bar = cursor.getCount();
                if(cursor.moveToFirst()) {
                    return cursor.getInt(0);
                } else
                	return cursor.getInt(0);

    }

    public String getEntry(int entry)
    {

        Cursor cursor = mDb.rawQuery(
                    "SELECT sensors FROM tblSensorData WHERE _id = " + entry, null);
        int bar = cursor.getCount();
        int rab = cursor.getPosition();
        boolean ret = cursor.moveToFirst();
        rab = cursor.getPosition();
        Cursor blat = fetchAllSensors();
        int bbar = blat.getCount();
        if(cursor.moveToFirst()) {
            return cursor.getString(0);
        } else
            return cursor.getString(0);

    }
    
    public String getRandomEntry()
    {
    	int id = 1;
    	id = getNumEntries();

    	int rand = random.nextInt(id) + 1;
        Cursor cursor = mDb.rawQuery(
                    "SELECT sensors FROM tblSensorData WHERE _id = " + rand, null);
                if(cursor.moveToFirst()) {
                    return cursor.getString(0);
                }
                return cursor.getString(0);

    }
}

